var net = require('net')

function probe(port, callback) {
  var server = net.createServer().listen(port)

  var calledOnce = false

  var timeoutRef = setTimeout(function () {
    calledOnce = true
    callback(false, port)
  }, 2000)

  timeoutRef.unref()

  var connected = false

  server.on('listening', function () {
    clearTimeout(timeoutRef)

    if (server)
      server.close()

    if (!calledOnce) {
      calledOnce = true
      callback(true, port)
    }
  })

  server.on('error', function (err) {
    clearTimeout(timeoutRef)

    var result = true
    if (err.code === 'EADDRINUSE')
      result = false

    if (!calledOnce) {
      calledOnce = true
      callback(result, port)
    }
  })
}



module.exports = probe
